package com.robaone.gwt.form.client.ui;

import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
/**
 * <pre>   Copyright Mar 23, 2012 Ansel Robateau
         http://www.robaone.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.</pre>
 * @author Ansel
 *
 */
public class CheckFieldUi extends Composite implements FormField {

	private static CheckFieldUiUiBinder uiBinder = GWT
			.create(CheckFieldUiUiBinder.class);

	interface CheckFieldUiUiBinder extends UiBinder<Widget, CheckFieldUi> {
	}

	public CheckFieldUi(String name) {
		initWidget(uiBinder.createAndBindUi(this));
		this.setName(name);
	}
	@UiField VerticalPanel content;
	private String m_name;
	private String m_stylename;
	private String m_addedname;
	@Override
	public void setStyleName(String name){
		content.setStyleName(name+"-panel");
		this.m_stylename = name;
	}
	@Override
	public void addStyleName(String name){
		content.addStyleName(name);
		this.m_addedname = name;
	}
	public void add(String check,boolean value){
		CheckBox box = new CheckBox();
		if(this.m_stylename != null){
			box.setStyleName(this.m_stylename);
		}
		if(this.m_addedname != null){
			box.addStyleName(this.m_addedname);
		}
		box.setName(getName());
		box.setText(check);
		box.setValue(value);
		content.add(box);
	}
	
	public String[] getValues(){
		Vector<String> retval = new Vector<String>();
		for(int i = 0; i < content.getWidgetCount();i++){
			Widget w = content.getWidget(i);
			if(w instanceof CheckBox){
				CheckBox box = (CheckBox)w;
				if(box.getValue()){
					retval.add(box.getText());
				}
			}
		}
		return retval.toArray(new String[0]);
	}

	public String getName() {
		return this.m_name;
	}
	public void setName(String name){
		this.m_name = name;
	}

	public void setValues(String[] values, String[] array) {
		for(int i = 0; i < values.length;i++){
			String item = array[i];
			boolean match = false;
			for(int j = 0; j < array.length;j++){
				String val = values[i];
				if(val.equals(item)){
					match = true;
					break;
				}
			}
			this.add(item, match);
		}
	}

	@Override
	public void setError(boolean b) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addKeyUpHandler(KeyUpHandler handler) {
		// TODO Auto-generated method stub
		
	}
}
